//
//  TeacherController.swift
//  BodyBuilding
//
//  Created by franze on 2017/9/30.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class TeacherController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var tableView:UITableView!
    var list:NSArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
        
        DispatchQueue.global(qos: .default).async {
            self.getList()
        }
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: "大师")
        navBar.backBtn.isHidden = true
        view.addSubview(navBar)
        
        tableView = UITableView(frame: CGRect(x: 0, y: 64, width: WIDTH, height: HEIGHT-113))
        tableView.register(ClubCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = view.backgroundColor
        tableView.tableFooterView = UIView()
        view.addSubview(tableView)
        
    }
    
    func getList(){
        let parameter:NSDictionary = ["SessionID":LoginModel.sessionID,"page":"1","mod":"master"]
        Networking.shared.getMasterList(parameter: parameter) { (json) in
            self.list = json
            self.tableView.reloadData()
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let dict = list![indexPath.row] as! NSDictionary
        cell.selectionStyle = .none
        
        let shape = CAShapeLayer()
        shape.frame = CGRect(x: 0, y: 3, width: WIDTH, height: 94)
        shape.backgroundColor = UIColor.white.cgColor
        shape.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: shape.bounds.width, height: shape.bounds.height)).cgPath
        shape.shadowColor = UIColor.gray.cgColor
        shape.shadowOffset = CGSize(width: 0, height: 0)
        shape.shadowOpacity = 0.4
        shape.shadowRadius = 2
        cell.contentView.layer.addSublayer(shape)
        
        let label = UILabel(frame: CGRect(x: WIDTH-100-25, y: 25, width: 100, height: 20))
        label.font = UIFont.systemFont(ofSize: 18)
        label.text = dict.value(forKey: "realname") as? String
        label.textAlignment = .right
        cell.contentView.addSubview(label)
        
        let btn = UIButton(frame: CGRect(x: WIDTH-100-25, y: 50, width: 100, height: 30))
        btn.setImage(UIImage(named:"love")?.scaleImageToSize(CGSize(width: 30, height: 30)), for: .normal)
        btn.setTitle(dict.value(forKey: "hits") as? String, for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.contentHorizontalAlignment = .right
        cell.contentView.addSubview(btn)
        
        let imgView = UIImageView(frame: CGRect(x: 15, y: 0, width: 80, height: 80))
        imgView.layer.cornerRadius = imgView.bounds.width/2
        imgView.layer.masksToBounds = true
        imgView.sd_setImage(with: URL(string: BB_IMAGEURLPRE+(dict.value(forKey: "thumb") as! String)), placeholderImage: UIImage(named:"placeholder"))
        imgView.center.y = 50
        cell.contentView.addSubview(imgView)

        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let controller = TeacherDetailController()
        controller.infoDict = list![indexPath.row] as! NSDictionary
        
        hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(controller, animated: true)
        hidesBottomBarWhenPushed = false
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
